#include "ftsatellitemap.h"

#define FTSAT_CAGE_SIZE 100.0

FTSatelliteMap::FTSatelliteMap() :
    QWidget()
{
    setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding );
}

void FTSatelliteMap::paintEvent(QPaintEvent *)
{
    QPainter painter(this);

    qreal side = qMin( width(), height() );
    painter.translate(width() / 2, height() / 2);
    painter.scale(side / (FTSAT_CAGE_SIZE*2.0), side / (FTSAT_CAGE_SIZE*2.0));

    painter.save();
    painter.setPen( QColor("black") );
    painter.setBrush( QBrush( QColor("blue") ) );
    painter.drawEllipse( QPointF(0,0),FTSAT_CAGE_SIZE, FTSAT_CAGE_SIZE );
    painter.restore();

    painter.save();
    painter.setPen( QColor("white") );
    painter.setBrush( QColor("red") );
    foreach (QGeoSatelliteInfo sat, satellites_visible)
    {
        painter.save();
        painter.rotate( sat.attribute( QGeoSatelliteInfo::Azimuth ) );
        qreal dist = cos( sat.attribute( QGeoSatelliteInfo::Elevation ) ) * FTSAT_CAGE_SIZE;
        painter.drawEllipse( QPoint(0, -dist ), 3,3);
        painter.restore();
    }
    painter.restore();

    painter.save();
    painter.setPen( QColor("white") );
    painter.setBrush( QColor("green") );
    foreach (QGeoSatelliteInfo sat, satellites_used)
    {
        painter.save();
        painter.rotate( sat.attribute( QGeoSatelliteInfo::Azimuth ) );
        qreal dist = cos( sat.attribute( QGeoSatelliteInfo::Elevation ) ) * FTSAT_CAGE_SIZE;
        painter.drawEllipse( QPoint(0, -dist ), 5,5);
        painter.restore();
    }
    painter.restore();

    QString text = QString("%1/%2").arg(satellites_used.count()).arg(satellites_visible.count());
    QRect text_rect = painter.boundingRect(0,0,0,0,0,text);
    painter.drawText( -text_rect.width()/2, -text_rect.height()/2, text );
}

void FTSatelliteMap::satelliteUpdateView( QList<QGeoSatelliteInfo> sats)
{
    satellites_visible = sats;
    update();
}

void FTSatelliteMap::satelliteUpdateUse( QList<QGeoSatelliteInfo> sats)
{
    satellites_used = sats;
    update();
}

void FTSatelliteMap::clearMap()
{
    satellites_used.clear();
    satellites_visible.clear();
    update();
}
